package com.rech.xbexpress3;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.rech.utils.OtherUtil;
import com.sjl.idcard.IdentityCardHandler;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;

public class ErrorActivity extends AppCompatActivity {
    public static ErrorActivity errorActivity;

    private Timer timer;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        errorActivity = this;

        SharedPreferences sharedPreferences = getSharedPreferences("xbbox", Context.MODE_PRIVATE);
        String orientation = sharedPreferences.getString("Orientation","");
        if (orientation.equals("landscape")) {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        } else {
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }

        if (!EventBus.getDefault().isRegistered(this)) {
            EventBus.getDefault().register(this);
        }
        setContentView(R.layout.activity_error);

        getSupportActionBar().hide();

        Button kd5_scan = (Button) findViewById(R.id.error_back);
        kd5_scan.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Exit();
                OtherUtil.restartApplication(ErrorActivity.this);

                }

        });


        initTimer();

        TextView tv_version = findViewById(R.id.tv_version);
        TextView tv_waiter = findViewById(R.id.tv_waiter);
        try {
            tv_version.setText("v" + OtherUtil.getVersion(this));
        } catch (Exception e) {
            e.printStackTrace();
        }
        String phone = sharedPreferences.getString("CabinetType_phone","");
        tv_waiter.setText("客服电话:" + phone);
    }
    private void initTimer(){


        timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {


                //心跳保持
                keepHeartBeat();
            }
        },0,1000);


    }
    private void keepHeartBeat(){
        Date date = new Date();
        int seconds = date.getSeconds();
        if((seconds%5) == 0) {
            new FetchHeartBeatTaskForError().execute();
        }
    }
    @Override
    protected void onDestroy() {

        super.onDestroy();
    }
    private void Exit(){
        EventBus.getDefault().unregister(this);
        //IdentityCardHandler.getInstance(this).release();
    }
    @Subscribe(threadMode = ThreadMode.MAIN,priority = 100,sticky = false)     //在UI线程执行，100  为优先级
    public void onEvent(Message message){
        Intent intent = new Intent();
        switch(message.what){
            case 10504:
                OtherUtil.restartApplication(this);
                break;

        }
    }
}
